<?php

namespace Biz\Services\Site;

use Lumen\Logger\Logger;
use Illuminate\Http\Request;
use Biz\Models\Site\SiteWelcome;
use Biz\Libraries\TranslationUtil;
use Illuminate\Support\Facades\DB;
use Biz\Criteria\Site\SiteFilterCriterion;
use Biz\Constants\Site\SiteWelcomeConstants;
use Lumen\Core\Exceptions\BusinessException;
use Biz\Repositories\Site\SiteWelcomeRepository;

/**
 * SiteWelcomeService.
 *
 * @license [http://www.85do.com] [杭州永奥网络科技有限公司]
 * @copyright Copyright (c) 2018-2026 Hangzhou Yongao Technology Co., Ltd. All rights reserved.
 */
class SiteWelcomeService
{
    /**
     * @var \Lumen\Repository\Contracts\RepositoryContract
     */
    protected $repository;

    /**
     * RepositoryController constructor.
     *
     * @param SiteWelcomeRepository $repository
     */
    public function __construct(SiteWelcomeRepository $repository)
    {
        $this->repository              = $repository;
    }

    /**
     * Get.
     *
     * @param Request $request
     *
     * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator|mixed
     */
    public function get(Request $request)
    {
        $site_id = $request->get('site_id');

        $welcome = $this->repository
            ->with(['translations'])
            ->pushCriterion(new SiteFilterCriterion($site_id))
            ->findFirst();

        return $welcome;
    }

    /**
     * Find By Site Id.
     *
     * @param int $site_id
     *
     * @throws BusinessException
     *
     * @return bool
     */
    public function findBySiteId(int $site_id)
    {
        $welcome = $this->repository
            ->pushCriterion(new SiteFilterCriterion($site_id))
            ->findFirst();

        return $welcome;
    }

    /**
     * Save.
     *
     * @param Request $request
     *
     * @throws \Exception
     *
     * @return SiteWelcome
     */
    public function save(Request $request): SiteWelcome
    {
        $data            = $request->all();

        $welcome = $this->findBySiteId($data['site_id']);

        if ($welcome instanceof SiteWelcome) {
            return $this->update($welcome, $data);
        }

        return $this->create($data);
    }

    /**
     * Create.
     *
     * @param array $data
     *
     * @throws \Exception
     *
     * @return SiteWelcome
     */
    public function create(array $data): SiteWelcome
    {
        // 1) Start Transaction
        DB::beginTransaction();
        try {
            // 创建介绍
            $data['status'] = SiteWelcomeConstants::STATUS_NORMAL;

            $data = TranslationUtil::convertTranslationsData($data);

            $welcome   = $this->repository->create($data);
            if (! $welcome) {
                throw new BusinessException(trans('site/welcome.create_fail'));
            }
            // Load Translations
            $welcome->load('translations');

            // 2) Commit Transaction
            DB::commit();
        } catch (\Exception $e) {
            // 3) Rollback Transaction
            DB::rollback();
            // 4) Log Transaction
            Logger::error(__CLASS__, __FUNCTION__, $e);
            // 5) Throw Exception
            throw $e;
        }

        return $welcome;
    }

    /**
     * Update.
     *
     * @param SiteWelcome $welcome
     * @param array       $data
     *
     * @throws \Exception
     *
     * @return SiteWelcome
     */
    public function update(SiteWelcome $welcome, array $data): SiteWelcome
    {
        // 1) Start Transaction
        DB::beginTransaction();
        try {
            $data = TranslationUtil::convertTranslationsData($data);

            $welcome   = $this->repository->update($welcome, $data);
            if (! $welcome) {
                throw new BusinessException(trans('site/welcome.create_fail'));
            }

            // 2) Commit Transaction
            DB::commit();
        } catch (\Exception $e) {
            // 3) Rollback Transaction
            DB::rollback();
            // 4) Log Transaction
            Logger::error(__CLASS__, __FUNCTION__, $e);
            // 5) Throw Exception
            throw $e;
        }

        return $welcome;
    }
}
